package com.shandao.shangxiu.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shandao.shangxiu.R;
import com.shandao.shangxiu.base.BaseActivity;
import com.shandao.shangxiu.bean.entity.BeanCountry;
import com.shandao.shangxiu.database.DbManager;
import com.shandao.shangxiu.net.API;
import com.shandao.shangxiu.util.Constants;
import com.shandao.shangxiu.util.Logger;
import com.shandao.shangxiu.util.SharedPre;
import com.shandao.shangxiu.util.SharedPreUtils;
import com.shandao.shangxiu.util.UIHelper;
import com.shandao.shangxiu.widget.Splash;
import com.umeng.message.PushAgent;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Created by Administrator on 2015/7/8.
 */
public class StartActivity extends Activity implements Animation.AnimationListener {
    private static final String TAG = StartActivity.class.getSimpleName();

    private ImageView ibStartPage;

    private DbManager manager;

    private PushAgent mPushAgent;// 友盟推送

    private boolean isGuide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        startPush();
        initView();
    }

    private void startPush() {
//        mPushAgent = PushAgent.getInstance(this);
//        mPushAgent.enable();
//        SharedPreUtils.putString(this, SharedPre.App.APP_DEVICEID,
//                mPushAgent.getRegistrationId());
    }

    private void initView() {
        isGuide = SharedPreUtils.getBoolean(this, SharedPre.App.ISGUIDE, true);
        manager = new DbManager(this);
        ibStartPage = (ImageView) findViewById(R.id.start_page);
        AlphaAnimation animation = new AlphaAnimation(0.5f, 1.0f);
        animation.setDuration(2000);
        animation.setAnimationListener(this);
        ibStartPage.startAnimation(animation);
    }

    @Override
    public void onAnimationStart(Animation animation) {
        if (isGuide) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        updateCity();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        addSplash();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }


    private void addSplash() {
        if (isGuide) {
            int[] resIds = {R.mipmap.splash_1, R.mipmap.splash_2,
                    R.mipmap.splash_3, R.mipmap.splash_4};
            Splash splash = new Splash(this);
            splash.setResIds(resIds, 0, 0);
            splash.show();
        } else {
            UIHelper.showLoginActivity(this);
            finish();
        }

    }


    private void updateCity() throws Exception {
        Gson gson = new Gson();
        List<BeanCountry> countries = gson.fromJson(new BufferedReader(new InputStreamReader
                (getAssets().open("country.json"))), new TypeToken<List<BeanCountry>>() {
        }.getType());
        manager.insertCities(countries);
        Logger.d(TAG, "读取city文件成功！！");
    }
}
